package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.PushMessage;
import mamikeji.liaowa.db.PushMessageManager;
import mamikeji.liaowa.widget.slidelistview.SliderView;

/**
 * Created by 廉冲-android开发 on 2017/5/17.
 */
public class Notice_systemAdapter extends BaseAdapter {
    List<PushMessage> mList;
    LayoutInflater inflater;
    Context context;
    SliderView slideView;
    OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onDeleteClick(int id);
    }

    public Notice_systemAdapter(List<PushMessage> mList, Context context) {
        this.mList = mList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public PushMessage getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        slideView = (SliderView) convertView;
        if (slideView == null) {
            convertView = inflater.inflate(R.layout.item_notice_system, parent, false);
            viewHolder.textView_notice_system_title = (TextView) convertView.findViewById(R.id.textView_notice_system_title);
            viewHolder.textView_notice_system_content = (TextView) convertView.findViewById(R.id.textView_notice_system_content);
            slideView = new SliderView(context);
            slideView.setContentView(convertView);
            slideView.setTag(viewHolder);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) slideView.getTag();
        }

        slideView.shrink();
        slideView.textView_delete.setText(R.string.user_notice_delete);

        PushMessage item = getItem(position);

        viewHolder.textView_notice_system_title.setText(item.getTitle());
        viewHolder.textView_notice_system_content.setText(item.getContent());

        slideView.setOnItemClickListener(new SliderView.OnItemClickListener() {
            @Override
            public void onDeleteClick() {
                mOnItemClickListener.onDeleteClick(position);
            }
        });
        return slideView;
    }

    public class ViewHolder {
        TextView textView_notice_system_title;
        TextView textView_notice_system_content;
    }

    public void remove(int position) {
        PushMessageManager.delete(mList.get(position));
        mList.remove(position);
        notifyDataSetChanged();
    }

    public void clear() {
        mList.clear();
        PushMessageManager.deleteAll();
        notifyDataSetChanged();
    }
}
